package zb.WX.service.menu;

import com.google.gson.Gson;
import org.junit.jupiter.api.Test;
import zb.WX.bean.menu.Menu;
import zb.WX.bean.menu.MenuFather;
import zb.WX.bean.menu.MenuSon;
import zb.WX.util.AccessTokenSaveUtil;
import zb.WX.util.GlobalVar;

import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


public class General {
    /*
     * 去执行一个https请求，设置一个自定义的菜单
     */
    @Test
    public void execGeneralMenu() {
        String access_token = AccessTokenSaveUtil.getLocationAccesstonken();
        String urlStr = GlobalVar.GENERALMENU.replace("ACCESS_TOKEN", access_token);
        try {
            //发送请求创建自定义菜单
            URL url = new URL(urlStr);
            HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();
            httpsConn.setDoOutput(true);
            OutputStreamWriter osw = new OutputStreamWriter(httpsConn.getOutputStream());
            osw.write(createGeneralMenu());
            osw.close();

            //输出返回的结果
            InputStreamReader isr = new InputStreamReader(httpsConn.getInputStream());
            char[] c = new char[1024];
            int len;
            String resultMsg = "";
            while ((len = isr.read(c)) != -1) {
                resultMsg += new String(c, 0, len);
            }
            System.out.println("创建自定义菜单返回结果:" + resultMsg);
            isr.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //创建一个自定义菜单
    /* 1：一级菜单的第一个
     * 2：一级菜单的第二个
     * 11：第一个一级菜单的第一个按钮
     * 22：第二个一级菜单中的第二个
     * 12:第一个一级菜单中的第二个
     */
    public String createGeneralMenu() {
        MenuSon menuSon11 = new MenuSon();
        menuSon11.setName("二级菜单1_click");
        menuSon11.setType("click");
        menuSon11.setKey("11");

        MenuSon menuSon12 = new MenuSon();
        menuSon12.setName("二级菜单2_view");
        menuSon12.setType("view");
        menuSon12.setUrl("http://www.baidu.com");
        menuSon12.setKey("12");

        MenuSon menuSon13 = new MenuSon();
        menuSon13.setName("二级菜单3_扫码");
        menuSon13.setType("scancode_push");
        menuSon13.setKey("13");

        MenuSon menuSon14 = new MenuSon();
        menuSon14.setName("二级菜单4_扫码2");
        menuSon14.setType("scancode_waitmsg");
        menuSon14.setKey("14");

        //创建第一个一级菜单
        MenuFather mf1 = new MenuFather();
        List<MenuSon> list1 = new ArrayList<MenuSon>();
        list1.add(menuSon11);
        list1.add(menuSon12);
        list1.add(menuSon13);
        list1.add(menuSon14);
        mf1.setName("一级菜单1");
        mf1.setList(list1);//添加二级菜单

        //===============================================
        MenuSon menuSon21 = new MenuSon();
        menuSon21.setName("二级菜单1_拍照或相册");
        menuSon21.setType("pic_photo_or_album");
        menuSon21.setKey("21");


        MenuSon menuSon22 = new MenuSon();
        menuSon22.setName("二级菜单2_发图器");
        menuSon22.setType("pic_weixin");
        menuSon22.setKey("22");

        MenuSon menuSon23 = new MenuSon();
        menuSon23.setName("二级菜单3_地理位置");
        menuSon23.setType("location_select");
        menuSon23.setKey("23");

        MenuSon menuSon24 = new MenuSon();
        menuSon24.setName("二级菜单3_音乐");
        menuSon24.setType("view");
        menuSon24.setUrl("http://v.qq.com/");
        menuSon24.setKey("24");

        //创建第二个一级级菜单
        MenuFather mf2 = new MenuFather();
        List<MenuSon> list2 = new ArrayList<MenuSon>();
        list2.add(menuSon21);
        list2.add(menuSon22);
        list2.add(menuSon23);
        list2.add(menuSon24);
        mf2.setName("一级菜单2");
        mf2.setList(list2);//添加二级菜单
        mf2.setKey("2");

        //============================
        //创建第三个一级菜单
        MenuFather mf3 = new MenuFather();
        mf3.setName("网页授权");
        mf3.setType("view");
        mf3.setUrl("http://zhangbin.xin/wxOAuth/guide.zb");
        mf3.setKey("3");

        //将一级菜单添加到菜单栏中
        Menu menu = new Menu();
        List<MenuFather> listFather = new ArrayList<MenuFather>();
        listFather.add(mf1);
        listFather.add(mf2);
        listFather.add(mf3);
        menu.setList(listFather);

        //将按钮对象转化为json输出
        Gson gson = new Gson();
        String menuJson = gson.toJson(menu);
        System.out.println("创建的自定义菜单：" + menuJson);
        return menuJson;

    }


    public String createmenu() {
        MenuSon menuSon11 = new MenuSon();
        menuSon11.setName("首页");
        menuSon11.setType("view");
        menuSon11.setUrl(zb.blog.base.global.GlobalVar.WEB_SITE);
        menuSon11.setKey("11");

        MenuSon menuSon12 = new MenuSon();
        menuSon12.setName("关于我");
        menuSon12.setType("view");
        menuSon12.setUrl(zb.blog.base.global.GlobalVar.WEB_SITE + "/frontstage/aboutMe.html");
        menuSon12.setKey("12");

        MenuSon menuSon13 = new MenuSon();
        menuSon13.setName("留言板");
        menuSon13.setType("view");
        menuSon13.setUrl(zb.blog.base.global.GlobalVar.WEB_SITE + "/frontstage/messageBoard.html");
        menuSon13.setKey("13");

        MenuSon menuSon14 = new MenuSon();
        menuSon14.setName("友链");
        menuSon14.setType("view");
        menuSon14.setUrl(zb.blog.base.global.GlobalVar.WEB_SITE + "/frontstage/friendLink.html");
        menuSon14.setKey("14");

        MenuFather mf1 = new MenuFather();
        List<MenuSon> list1 = new ArrayList<MenuSon>();
        list1.add(menuSon11);
        list1.add(menuSon12);
        list1.add(menuSon13);
        list1.add(menuSon14);
        mf1.setName("个人博客");
        mf1.setList(list1);
        mf1.setKey("1");

        MenuFather mf2 = new MenuFather();
        mf2.setName("公告");
        mf2.setType("click");
        mf2.setKey("2");

        MenuFather mf3 = new MenuFather();
        mf3.setName("登录");
        mf3.setType("view");
        mf3.setUrl(zb.blog.base.global.GlobalVar.WEB_SITE + "/login.html");
        mf3.setKey("3");

        Menu menu = new Menu();
        List<MenuFather> listFather = new ArrayList<MenuFather>();
        listFather.add(mf1);
        listFather.add(mf2);
        listFather.add(mf3);
        menu.setList(listFather);

        Gson gson = new Gson();
        String menuJson = gson.toJson(menu);
        System.out.println("创建的自定义菜单：" + menuJson);
        return menuJson;
    }

}
